package edu.neusoft.springhtmldemo.util;

import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

@Component
public class StringToDate implements Converter<String, Timestamp> {

    @Override
    public Timestamp convert(String s) {
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df2.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        Date date = null;
        try {
            date = df.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String gpsTime = df2.format(date);
        Timestamp timestamp=Timestamp.valueOf(gpsTime);
        return timestamp;
    }
}
